//
//  ToolbarView.swift
//  CooImage
//
//  Created by jerry on 2025/8/16.
//

import SwiftUI

struct ToolbarView: View {
    var zoomIn: () -> Void
    var zoomOut: () -> Void
    var resetView: () -> Void
    var toggleToolbar: () -> Void
    var rotateClockwise: () -> Void = {}
    var rotateCounterClockwise: () -> Void = {}
    var previousImage: () -> Void = {}
    var nextImage: () -> Void = {}
    var hasPreviousImage: Bool = false
    var hasNextImage: Bool = false
    var requestDirectoryAccess: (() -> Void)? = nil
    var showDirectoryAccessButton: Bool = false
    
    var body: some View {
        HStack {
            // 图片导航按钮
            Button(action: {
                previousImage()
            }) {
                Image(systemName: "chevron.left")
                    .frame(width: 20, height: 20)
            }
            .buttonStyle(.borderless)
            .help("上一张图片 (←)")
            .disabled(!hasPreviousImage)
            
            Button(action: {
                nextImage()
            }) {
                Image(systemName: "chevron.right")
                    .frame(width: 20, height: 20)
            }
            .buttonStyle(.borderless)
            .help("下一张图片 (→)")
            .disabled(!hasNextImage)
            
            Divider()
                .frame(height: 16)
                .padding(.horizontal, 4)
            
            // 缩放按钮
            Button(action: {
                zoomIn()
            }) {
                Image(systemName: "plus.magnifyingglass")
                    .frame(width: 20, height: 20)
            }
            .buttonStyle(.borderless)
            .help("放大 (⌘+)")
            
            Button(action: {
                zoomOut()
            }) {
                Image(systemName: "minus.magnifyingglass")
                    .frame(width: 20, height: 20)
            }
            .buttonStyle(.borderless)
            .help("缩小 (⌘-)")
            
            Button(action: {
                resetView()
            }) {
                Image(systemName: "arrow.counterclockwise")
                    .frame(width: 20, height: 20)
            }
            .buttonStyle(.borderless)
            .help("重置视图 (⌘0)")
            
            Divider()
                .frame(height: 16)
                .padding(.horizontal, 4)
            
            // 旋转按钮
            Button(action: {
                rotateCounterClockwise()
            }) {
                Image(systemName: "rotate.left")
                    .frame(width: 20, height: 20)
            }
            .buttonStyle(.borderless)
            .help("逆时针旋转 (⇧⌘R)")
            
            Button(action: {
                rotateClockwise()
            }) {
                Image(systemName: "rotate.right")
                    .frame(width: 20, height: 20)
            }
            .buttonStyle(.borderless)
            .help("顺时针旋转 (⌘R)")
            
            // 目录访问按钮
            if showDirectoryAccessButton {
                Divider()
                    .frame(height: 16)
                    .padding(.horizontal, 4)
                
                Button(action: {
                    requestDirectoryAccess?()
                }) {
                    Image(systemName: "folder.badge.plus")
                        .frame(width: 20, height: 20)
                }
                .buttonStyle(.borderless)
                .help("浏览同目录下的其他图片")
            }
            
            Spacer()
            
            Button(action: {
                toggleToolbar()
            }) {
                Image(systemName: "chevron.compact.up")
                    .frame(width: 20, height: 20)
            }
            .buttonStyle(.borderless)
            .help("隐藏工具栏")
        }
        .padding(.horizontal)
        .padding(.vertical, 8)
        .background(Color(.controlBackgroundColor))
        .overlay(
            Rectangle()
                .frame(height: 1)
                .foregroundColor(Color(.separatorColor)),
            alignment: .bottom
        )
    }
}

struct ToolbarView_Previews: PreviewProvider {
    static var previews: some View {
        ToolbarView(
            zoomIn: {},
            zoomOut: {},
            resetView: {},
            toggleToolbar: {}
        )
    }
}